//
//  Team9WorkHeaderView.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/17.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9WorkHeaderView.h"
#import "Team9Header.h"
#import "Team9JQHeader.h"
#import "Team9ExpertPro_serviceModel.h"
#import "Team9ExpertUser_home_infoModel.h"
#import "Team9SaveModel.h"
#import "DataBaseTool.h"

@interface Team9WorkHeaderView ()
@property(nonatomic, strong)Team9BaseView *headView;
@property(nonatomic, strong)UIImageView *faceImageView;
@property(nonatomic, strong)Team9BaseLabel *workCountLabel;
@property(nonatomic, strong)Team9BaseLabel *reViewLabel;
@property(nonatomic, strong)UILabel *typeLabel;
@property(nonatomic, strong)UIView *lineView;
@property(nonatomic, strong)Team9BaseLabel *nameLabel;
@property(nonatomic, strong)UIButton *collectionButton;

@property(nonatomic, strong)Team9BaseView *footView;
@property(nonatomic, strong)Team9BaseLabel *loctionLabel;
@property(nonatomic, strong)Team9BaseLabel *costLabel;
// 新增
@property(nonatomic, strong)Team9BaseLabel *native_placeLabel;
@property(nonatomic, strong)NSMutableArray *starImageViews;
@property(nonatomic, strong)Team9BaseLabel *ratingsLabel;
@property(nonatomic, strong)Team9BaseLabel *judgelabel;

@property(nonatomic, strong)Team9BaseLabel *rangelabel;
@property(nonatomic, strong)UIButton *detialButton;
@property(nonatomic, copy) NSString *ratings;

@property(nonatomic, strong)Team9SaveModel *saver;
@end

@implementation Team9WorkHeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
#pragma mark string mark by a array only with string
- (NSString *)stringByArray:(NSArray *)array{
    NSString *string = [NSString string];
    for (NSString *str in array) {
        string = [string stringByAppendingString:[NSString stringWithFormat:@"%@ ", str]];
    }
    return string;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
        self.serviceModel = [Team9ExpertPro_serviceModel new];
        self.userModel = [Team9ExpertUser_home_infoModel new];
//                [self test];
    }
    return self;
}
- (void)changecolor
{
    if ([Team9EveningType shareEvening].eveningFailling) {
        self.backgroundColor = [UIColor blackColor];
    } else {
        self.backgroundColor = [UIColor whiteColor];
    }
}
- (void)createView{
//    self.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
    
    self.headView = [[Team9BaseView alloc] init];
    [self addSubview:_headView];
//    self.headView.backgroundColor = [UIColor whiteColor];
    
    self.faceImageView = [UIImageView new];
    [self addSubview:_faceImageView];
    
    self.nameLabel = [[Team9BaseLabel alloc] init];
    self.nameLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    [self addSubview:_nameLabel];
    
    self.ratingsLabel = [[Team9BaseLabel alloc] initWith:LableColorTypeRedCyan];
    self.ratingsLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    [self addSubview:_ratingsLabel];
    
    self.judgelabel = [[Team9BaseLabel alloc] init];
    [self addSubview:_judgelabel];
    self.judgelabel.font = [UIFont systemFontOfSize:12 * WIDTH / 375];
    
    self.typeLabel = [[UILabel alloc] init];
    [self addSubview:_typeLabel];
    self.typeLabel.textColor = [UIColor whiteColor];
    self.typeLabel.backgroundColor = [UIColor magentaColor];
    self.typeLabel.font = [UIFont systemFontOfSize:12 * WIDTH / 375];
    
    self.lineView = [UIView new];
    self.lineView.backgroundColor = [UIColor lightGrayColor];
    [self addSubview:_lineView];
    
    self.workCountLabel = [[Team9BaseLabel alloc] initWith:LableColorTypeRedCyan];
    [self addSubview:_workCountLabel];
    self.workCountLabel.font = [UIFont systemFontOfSize:15 * WIDTH / 375];
    
    self.reViewLabel = [[Team9BaseLabel alloc] initWith:LableColorTypeRedCyan];
    [self addSubview:_reViewLabel];
    self.reViewLabel.font = [UIFont systemFontOfSize:15 * WIDTH / 375];
    
    self.collectionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.collectionButton setTitle:@"关注" forState:UIControlStateNormal];
    //    [self.collectionButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.collectionButton.backgroundColor = [UIColor orangeColor];
    self.collectionButton.layer.cornerRadius = 5;
    self.collectionButton.layer.masksToBounds = YES;
    self.collectionButton.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.collectionButton.titleLabel.font = [UIFont systemFontOfSize:15 * WIDTH / 375];
    [self addSubview:_collectionButton];
    
    
    
    self.starImageViews = @[].mutableCopy;
    for (int i = 0; i < 5; i++) {
        UIImageView *starImageView = [UIImageView new];
        [self.starImageViews addObject:starImageView];
        [self addSubview:starImageView];
//        starImageView.image = [UIImage imageNamed:@"halfStar"];
    }
    
    [self createFoot];
    
    [self p_contrains];
}
- (void)createFoot{
    self.footView = [[Team9BaseView alloc] init];
    [self addSubview:_footView];
//    self.footView.backgroundColor = [UIColor whiteColor];
    
    self.loctionLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:_loctionLabel];
    
    self.native_placeLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:_native_placeLabel];
    
    self.costLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:_costLabel];
    
    self.rangelabel = [[Team9BaseLabel alloc] init];
    [self addSubview:_rangelabel];
    
    self.detialButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.detialButton setTitle:@"查看全部信息" forState:UIControlStateNormal];
//    [self.detialButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [self addSubview:_detialButton];
    self.detialButton.titleLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    self.detialButton.layer.borderColor = [UIColor orangeColor].CGColor;
    self.detialButton.layer.borderWidth = 1.0f;
    self.detialButton.layer.cornerRadius = 5.0f;
    [self.detialButton addTarget:self action:@selector(detail:) forControlEvents:UIControlEventTouchUpInside];
    
    _loctionLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    _costLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    _rangelabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    _native_placeLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
}
- (void)detail:(id)sender{
    if (self.block) {
        self.block();
    }
}
- (void)p_contrains{
    
    UIImageView *lastStarView = nil;
    for (UIImageView *imageView in self.starImageViews) {
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.ratingsLabel.mas_top);
            if (lastStarView) {
                make.right.equalTo(lastStarView.mas_left).offset(-1);
            } else make.right.equalTo(self.ratingsLabel.mas_left).offset(-5);
            make.width.equalTo(@(WIDTH / 375 * 14));
            make.height.equalTo(imageView.mas_width);
        }];
        lastStarView = imageView;
    }
    
    
    [self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.equalTo(self);
        make.bottom.equalTo(self.collectionButton).offset(10);
    }];
    
    [self.faceImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.height.and.width.equalTo(@(100 * WIDTH / 375));
        make.top.equalTo(self.headView).offset(10);
    }];
    self.faceImageView.layer.cornerRadius = 50 * WIDTH / 375;
    self.faceImageView.layer.masksToBounds = YES;
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.top.equalTo(self.faceImageView.mas_bottom).offset(10);
    }];
    
    [self.typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.nameLabel.mas_right).offset(5);
        make.centerY.equalTo(self.nameLabel);
    }];
    
    [self.ratingsLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.lineView);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(15);
    }];
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.width.equalTo(@1);
        make.top.equalTo(self.ratingsLabel.mas_bottom).offset(5);
    }];
    
    Team9BaseLabel *conutLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:conutLabel];
    conutLabel.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    conutLabel.text = @"套作品";
    
    Team9BaseLabel *reViewLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:reViewLabel];
    reViewLabel.font = [UIFont systemFontOfSize:10 * WIDTH / 375];
    reViewLabel.text = @"人预约过";
    
    [self.workCountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView);
    }];
    [self.reViewLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView);
    }];
    [conutLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.workCountLabel.mas_bottom).offset(1);
        make.centerX.equalTo(self.workCountLabel);
        make.right.equalTo(self.lineView).offset(-10);
        make.bottom.equalTo(self.lineView);
    }];
    [reViewLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.lineView);
        make.centerX.equalTo(self.reViewLabel);
        make.left.equalTo(self.lineView).offset(10);
    }];
    
    [self.collectionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.lineView.mas_bottom).offset(10);
        make.centerX.equalTo(self);
        make.width.equalTo(self.collectionButton.mas_height).multipliedBy(2);
    }];
    [self p_contraintsFoot];
    [self.judgelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.ratingsLabel);
        make.left.equalTo(self.ratingsLabel.mas_right).offset(10);
    }];
    
    
}
- (void)p_contraintsFoot{
    [self.footView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headView.mas_bottom).offset(10);
        make.bottom.right.left.equalTo(self);
    }];
    Team9BaseLabel *loctionNameLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:loctionNameLabel];
    
    Team9BaseLabel *costNameLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:costNameLabel];
    
    Team9BaseLabel *native_placeLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:native_placeLabel];
    
    Team9BaseLabel *rangeNameLabel = [[Team9BaseLabel alloc] init];
    [self addSubview:rangeNameLabel];
    
    loctionNameLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    costNameLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    rangeNameLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    native_placeLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
    
    loctionNameLabel.text = @"服务地区";
    costNameLabel.text = @"施工经验";
    rangeNameLabel.text = @"施工方式";
    native_placeLabel.text = @"籍贯";
    
    [loctionNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.footView).offset(10);
    }];
    [native_placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loctionNameLabel.mas_bottom).offset(10);
        make.left.equalTo(loctionNameLabel);
    }];
    
    [costNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(native_placeLabel.mas_bottom).offset(10);
        make.right.equalTo(loctionNameLabel);
    }];
    
    [rangeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(costNameLabel.mas_bottom).offset(10);
        make.right.equalTo(costNameLabel);
    }];
    
    [self.loctionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(loctionNameLabel);
        make.left.equalTo(loctionNameLabel.mas_right).offset(10);
    }];
    [self.native_placeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(native_placeLabel);
        make.left.equalTo(loctionNameLabel.mas_right).offset(10);
    }];
    [self.costLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(costNameLabel);
        make.left.equalTo(costNameLabel.mas_right).offset(10);
    }];
    [self.rangelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(rangeNameLabel);
        make.left.equalTo(rangeNameLabel.mas_right).offset(10);
    }];
    [self.detialButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(rangeNameLabel.mas_bottom).offset(10);
        make.centerX.equalTo(self);
        make.width.mas_equalTo(self.detialButton.mas_height).multipliedBy(3.5);
    }];
}
- (void)reload{
    UIColor *color;
    if ([Team9EveningType shareEvening].eveningFailling) {
        color = [UIColor grayColor];
    } else color = [UIColor orangeColor];
    [self.detialButton setTitleColor:color forState:UIControlStateNormal];
    self.collectionButton.backgroundColor = color;
    self.detialButton.layer.borderColor = color.CGColor;
    
    self.ratings = self.pro_ratings[@"pro_ratings"];
    if (!self.ratings) {
        self.ratings = @"0.0";
    }
    self.loctionLabel.text = self.serviceModel.location;
    self.native_placeLabel.text = self.serviceModel.native_place;
    self.rangelabel.text = [self stringByArray:self.serviceModel.services];
    self.costLabel.text = [self.serviceModel.exp_years stringByAppendingString:@"年"];
    
    [self.faceImageView sd_setImageWithURL:[NSURL URLWithString:self.userModel.face] placeholderImage:[UIImage imageNamed:@"333"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        [UIView animateWithDuration:0.3 animations:^{
            CATransition *transition = [CATransition animation];
            transition.type = @"fade";
            [transition setDuration:0.5];
            [transition setRepeatCount:1];
            // 向imageView上添加动画效果
            [self.faceImageView.layer addAnimation:transition forKey:@"transition"];
        }];
    }];
    
    self.nameLabel.text = self.userModel.nick;
    switch (self.userModel.user_type.intValue) {
        case 2:
            self.typeLabel.text = @"工长";
            break;
        default:
            break;
    }
    self.reViewLabel.text = self.userModel.verified_work_count;
    self.workCountLabel.text = self.userModel.contacted_count;
    
    CGFloat ratings = self.ratings.floatValue;
    NSInteger wholeStar = (NSInteger)ratings;
    BOOL halfStar = (ratings - wholeStar) >= 0.5;
    for (NSInteger i = 5; i > 0; i--) {
        UIImageView *starImageView = self.starImageViews[i - 1];
        starImageView.image = [UIImage imageNamed:@"emptyStar"];
        if (i > 5 - wholeStar) {
            starImageView.image = [UIImage imageNamed:@"oneStar"];
        }
        if (i == 5 - wholeStar && halfStar) {
            starImageView.image = [UIImage imageNamed:@"halfStar"];
        }
    }
    self.ratingsLabel.text = self.ratings;
   
    self.judgelabel.text = [NSString stringWithFormat:@"%d条评价", self.userModel.review_count.intValue];
    self.saver = [Team9SaveModel new];
    self.saver.author_id = self.userId;
    self.saver.author_nick = self.userModel.nick;
    self.saver.author_type = @"2";
    self.saver.author_face = self.userModel.face;
    DataBaseTool *dataTool = [DataBaseTool shareDataBaseTool];
    if ([dataTool isHave:self.saver] == InTable) {
        [self.collectionButton setTitle:@"已关注" forState:UIControlStateNormal];
        [self.collectionButton addTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
    } else if ([dataTool isHave:self.saver] == NotInTable){
        [self.collectionButton setTitle:@"关注" forState:UIControlStateNormal];
        [self.collectionButton addTarget:self action:@selector(saveData:) forControlEvents:UIControlEventTouchUpInside];
    } else [self.collectionButton setTitle:@"错误" forState:UIControlStateNormal];
    
}
- (void)deleteData:(UIButton *)sender{
    [sender removeTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
    [[DataBaseTool shareDataBaseTool] deleteDesigner:self.saver];
    [self.collectionButton setTitle:@"关注" forState:UIControlStateNormal];
    [self.collectionButton addTarget:self action:@selector(saveData:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)saveData:(UIButton *)sender{
    [sender removeTarget:self action:@selector(saveData:) forControlEvents:UIControlEventTouchUpInside];
    [[DataBaseTool shareDataBaseTool] insertDesigner:self.saver];
    [self.collectionButton setTitle:@"已关注" forState:UIControlStateNormal];
    [self.collectionButton addTarget:self action:@selector(deleteData:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)test{
    self.faceImageView.backgroundColor = [UIColor redColor];
    self.nameLabel.text = @"我是谁";
    self.native_placeLabel.text = @"??";
    self.workCountLabel.text = @"36";
    self.reViewLabel.text = @"24";
    self.typeLabel.text = @"设计师";
    self.loctionLabel.text = @"china";
    self.costLabel.text = @"¥123";
    self.rangelabel.text = @"打脸";
    self.ratingsLabel.text = @"12";
}

@end
